/**
 * @overview 创建点
 * @author dengqiang
 * @description 鲸云CgsMark
 * @update  2022.03.21
 */
class GPoint {
  constructor() {
    window.GPointArray = []
  }

  //新增单个点
  add(paramObj) {
    const { name, parentName, option } = paramObj
    const optionModified = {
      position: option.position,
      // attribute: option.attribute,
      name: option.name,
      url: option.url,
      positioning: 'center-bottom',
      scale: option.scale || 0.6,
      pixelOffset: option.pixelOffset || [0, -20],
    }
    const point = new CgsMark(optionModified)
    point.feature3D.attribute = option.attribute //支持添加自定义属性
    point.setVisibleHeight(option.visibleHeight || [0, 300000000])
    point.setLabelOffset([3, 0])
    point.setFont('600 14px 微软雅黑')
    point.setLabelFillColor(option.color || '#000')
    point.setLabelOutlineColor('#fff')
    point.setLabelOutlineWidth(1)
    point.feature3D.label.setVisibleHeight(option.labelVisibleHeight || [0, 5000])
    point.feature3D.entity.billboard.disableDepthTestDistance = Number.MAX_SAFE_INTEGER
    point.feature3D.label.entity.label.disableDepthTestDistance = Number.POSITIVE_INFINITY // label不被地形遮挡

    const pointObject = {
      name: name,
      parentName: parentName,
      point: point,
    }
    window.GPointArray.push(pointObject)
    window.map.addFeature(point)
  }

  //根据name移除单个点
  removeByName(name) {
    window.GPointArray.forEach((item, index) => {
      if (item.name == name) {
        window.map.removeFeature(item.point)
        window.GPointArray.splice(index, 1)
      }
    })
  }

  //根据parentName移除一批点
  removeByPatName(parentName) {
    const length = window.GPointArray.length
    for (let i = length - 1; i >= 0; i--) {
      if (window.GPointArray[i].parentName == parentName) {
        window.map.removeFeature(window.GPointArray[i].point)
        window.GPointArray.splice(i, 1)
      }
    }
  }

  //移除所有点
  removeAll() {
    window.GPointArray.forEach((item) => {
      window.map.removeFeature(item.point)
    })
    window.GPointArray = []
  }

  // removeSinglePoint(name) {
  // 	window.GPointArray.forEach((item, index) => {
  // 		if (item.name == name) {
  // 			window.map.removeFeature(item.point)
  // 			window.GPointArray.splice(index, 1)
  // 		}
  // 	})
  // }

  // removeAllPoint() {
  // 	window.GPointArray.forEach((item, index) => {
  // 		window.map.removeFeature(item.point);
  // 	})
  // 	window.GPointArray = [];
  // }

  //根据name控制单个点的显隐
  setVisibleByName(name, boolean) {
    window.GPointArray.forEach((item) => {
      if (item.name == name) {
        item.point.setVisible(boolean)
      }
    })
  }

  //根据parentName控制一批点的显隐
  setVisibleByPatName(parentName, boolean) {
    window.GPointArray.forEach((item) => {
      if (item.parentName == parentName) {
        item.point.setVisible(boolean)
      }
    })
  }

  //根据name设置单个点的位置
  setPosition(name, newPosition) {
    window.GPointArray.forEach((item, index) => {
      if (item.name == name) {
        item.point.setPosition(newPosition)
      }
    })
  }
}

export default GPoint
